home *** CD-ROM | disk | FTP | other *** search
- class CBody extends MovieClip implements ISimulated
- {
- var m_velocity;
- var m_acceleration;
- var m_drag;
- var m_forceAccumulator;
- var m_impulseAccumulator;
- var m_lastLocation;
- var m_collisionVolume;
- var m_arrCollisionBuckets;
- var m_attachedBodies;
- var m_effectiveVelocity;
- var m_isAttached;
- var _location;
- var m_mass = 1;
- var m_restitution = 1;
- var m_dragLinear = 0.8;
- var m_dragAngular = 0.8;
- var m_maxSpeed = -1;
- var m_doRotateAttached = false;
- var m_doUpdateRotation = true;
- var m_doesCollide = true;
- var m_isSimulating = true;
- var m_nUpdates = 0;
- var m_nImpulses = 0;
- var m_angularVelocity = 0;
- var m_angularAcceleration = 0;
- var m_torqueAccumulator = 0;
- var m_lastUpdateTime = 0;
- var m_maxOldLocations = 3;
- var m_didMove = false;
- function CBody()
- {
- super();
- FreshDebug.Assert(FreshFramework._simulation != undefined,"FreshFramework._simulation");
- this.m_velocity = new Vector2D(0,0);
- this.m_acceleration = new Vector2D(0,0);
- this.m_drag = new Vector2D(0,0);
- this.m_forceAccumulator = new Vector2D(0,0);
- this.m_impulseAccumulator = new Vector2D(0,0);
- this.m_lastLocation = new Vector2D(this._x,this._y);
- this.m_lastUpdateTime = FreshFramework._simulation.GetNamedClock(this.GetClockName());
- if(!this.m_collisionVolume)
- {
- this.CreateDefaultCollisionVolume();
- }
- FreshFramework._simulation.RegisterBody(this);
- this.m_arrCollisionBuckets = new Array();
- this.m_attachedBodies = new Array();
- }
- function Destroy()
- {
- FreshFramework._simulation.UnregisterBody(this);
- this.VacatePriorCollisionBuckets();
- this.swapDepths(0);
- this.removeMovieClip();
- }
- function CreateDefaultCollisionVolume()
- {
- this.m_collisionVolume = new CCircle(this,Math.max(this._width,this._height) * 0.5);
- }
- function get _updatePriority()
- {
- return 0;
- }
- function get _location()
- {
- return new Vector2D(this._x,this._y);
- }
- function set _location(loc)
- {
- this._x = loc._x;
- this._y = loc._y;
- }
- function GetLocation()
- {
- return new Vector2D(this._x,this._y);
- }
- function GetWorldSpaceLocation()
- {
- var _loc4_ = this._location;
- var _loc3_ = this._parent;
- while(_loc3_ != _root)
- {
- _loc4_.Add(new Vector2D(_loc3_._x,_loc3_._y));
- _loc3_ = _loc3_._parent;
- }
- return _loc4_;
- }
- function GetWorldSpaceRotation()
- {
- var _loc4_ = this._rotation;
- var _loc3_ = this._parent;
- while(_loc3_ != _root)
- {
- _loc4_ += _loc3_._rotation;
- _loc3_ = _loc3_._parent;
- }
- return _loc4_;
- }
- function get _lastLocation()
- {
- return this.m_lastLocation;
- }
- function get _velocity()
- {
- return this.m_velocity.GetCopy();
- }
- function get _effectiveVelocity()
- {
- return this.m_effectiveVelocity;
- }
- function get _acceleration()
- {
- return this.m_acceleration;
- }
- function get _forceAccumulator()
- {
- return this.m_forceAccumulator.GetCopy();
- }
- function get _speed()
- {
- return this.m_velocity.MagnitudeSafe();
- }
- function get _doesCollide()
- {
- return this.m_collisionVolume && this.m_doesCollide;
- }
- function get _didMove()
- {
- return this.m_didMove;
- }
- function get _topLeftCorner()
- {
- if(this.m_collisionVolume)
- {
- return this.m_collisionVolume._topLeftCorner;
- }
- return this._location;
- }
- function get _bottomRightCorner()
- {
- if(this.m_collisionVolume)
- {
- return this.m_collisionVolume._bottomRightCorner;
- }
- return this._location;
- }
- function get _boundingRadius()
- {
- if(this.m_collisionVolume)
- {
- return this.m_collisionVolume._boundingRadius;
- }
- return Math.max(this._width * 0.5,this._height * 0.5);
- }
- function get _maxSpeed()
- {
- return this.m_maxSpeed;
- }
- function get _angularVelocity()
- {
- return this.m_angularVelocity;
- }
- function get _nUpdates()
- {
- return this.m_nUpdates;
- }
- function get _arrCollisionBuckets()
- {
- return this.m_arrCollisionBuckets;
- }
- function AttachBody(body)
- {
- if(body)
- {
- this.m_attachedBodies.push(body);
- body.m_isAttached = true;
- }
- }
- function NotifyAddedToBucket(bucket)
- {
- this.m_arrCollisionBuckets.push(bucket);
- }
- function VacatePriorCollisionBuckets()
- {
- var _loc3_ = this.m_arrCollisionBuckets.length;
- var _loc2_ = 0;
- while(_loc2_ < _loc3_)
- {
- this.m_arrCollisionBuckets[_loc2_].RemoveBody(this);
- _loc2_ = _loc2_ + 1;
- }
- this.m_arrCollisionBuckets.splice(0);
- }
- function NotifyCollision(collisionInfo)
- {
- }
- function ApplyForce(vecForce)
- {
- if(this.m_mass > 0)
- {
- this.m_forceAccumulator.Add(vecForce.GetDivideScalar(this.m_mass));
- }
- }
- function ApplyThrust(vecForce)
- {
- var _loc2_ = new Vector2D(vecForce._x,vecForce._y);
- _loc2_.Rotate(MathUtil.DegreesToRadians(this._rotation));
- this.ApplyForce(_loc2_);
- }
- function ApplyImpulse(vecImpulse)
- {
- if(this.m_mass > 0)
- {
- this.m_nImpulses = this.m_nImpulses + 1;
- this.m_impulseAccumulator.Add(vecImpulse);
- }
- }
- function ApplyGravity(gravity)
- {
- if(this.m_mass > 0)
- {
- this.m_forceAccumulator.Add(gravity);
- }
- }
- function ApplyTorque(torque)
- {
- if(this.m_mass > 0)
- {
- this.m_torqueAccumulator += torque;
- }
- }
- function GetClockName()
- {
- return "";
- }
- function PreUpdate()
- {
- this.m_didMove = false;
- }
- function Update()
- {
- this.m_nUpdates = this.m_nUpdates + 1;
- var _loc4_ = this.GetClockName();
- var _loc3_ = FreshFramework._simulation.GetNamedDeltaTime(_loc4_);
- if(!this.m_isAttached && this.m_isSimulating && this.m_mass > 0 && !FreshFramework._simulation.GetNamedIsPaused(_loc4_) && _loc3_ > 0)
- {
- this.m_lastUpdateTime = FreshFramework._simulation.GetNamedClock(_loc4_);
- if(this.m_doUpdateRotation)
- {
- this.m_angularAcceleration = this.m_torqueAccumulator;
- this.m_angularVelocity += this.m_angularAcceleration * _loc3_;
- this.m_angularVelocity -= this.m_angularVelocity * Math.min(this.m_dragAngular * _loc3_,1);
- this._rotation += this.m_angularVelocity * _loc3_;
- }
- if(this.m_nImpulses > 0)
- {
- this.m_impulseAccumulator.DivideScalar(this.m_nImpulses);
- this.m_velocity.Set(this.m_impulseAccumulator);
- }
- this.m_acceleration.Set(this.m_forceAccumulator);
- this.m_velocity.Add(this.m_acceleration.GetMultiplyScalar(_loc3_));
- this.m_velocity.Subtract(this.m_velocity.GetMultiplyScalar(Math.min(this.m_dragLinear * _loc3_,1)));
- if(this.m_drag.MagnitudeSquared() > 0)
- {
- var _loc5_ = MathUtil.DegreesToRadians(this._rotation);
- this.m_velocity.Rotate(- _loc5_);
- this.m_velocity.Subtract(this.m_velocity.GetMultiply(this.m_drag.GetMultiplyScalar(_loc3_)));
- this.m_velocity.Rotate(_loc5_);
- }
- if(this._maxSpeed >= 0 && this._speed > this._maxSpeed)
- {
- this.m_velocity.Normalize();
- this.m_velocity.MultiplyScalar(this._maxSpeed);
- }
- if(this.m_velocity.MagnitudeSquared() < 0.01)
- {
- this.m_velocity.SetToZero();
- }
- this._x += this.m_velocity._x * _loc3_;
- this._y += this.m_velocity._y * _loc3_;
- var _loc2_ = 0;
- while(_loc2_ < this.m_attachedBodies.length)
- {
- this.m_attachedBodies[_loc2_]._location = this._location;
- if(this.m_doRotateAttached)
- {
- this.m_attachedBodies[_loc2_]._rotation = this._rotation;
- }
- _loc2_ = _loc2_ + 1;
- }
- }
- }
- function UpdateConstraints()
- {
- }
- function PostUpdate()
- {
- this.m_didMove = this.m_nUpdates == 1 || this._location.GetDistanceSquared(this.m_lastLocation) > 0.0001;
- this.m_effectiveVelocity = this._location.GetSubtract(this.m_lastLocation);
- this.m_lastLocation = this._location;
- this.m_forceAccumulator.SetToZero();
- this.m_impulseAccumulator.SetToZero();
- this.m_nImpulses = 0;
- this.m_torqueAccumulator = 0;
- }
- function Rollback(amount)
- {
- this._location = this.m_lastLocation;
- }
- function GetDelta(body)
- {
- return new Vector2D(this._x - body._x,this._y - body._y);
- }
- function GetDistance(body)
- {
- return this.GetDelta(body).Magnitude();
- }
- function GetDistanceSquared(body)
- {
- return this.GetDelta(body).MagnitudeSquared();
- }
- function DoesSuppressCentralizedCollisionResolution()
- {
- return false;
- }
- function DoesCollideWith(body)
- {
- if(!this._doesCollide || !body._doesCollide)
- {
- return null;
- }
- var _loc2_ = Intersection.DoIntersect(this.m_collisionVolume,body.m_collisionVolume);
- if(_loc2_)
- {
- _loc2_.m_body1 = this;
- _loc2_.m_body2 = body;
- }
- return _loc2_;
- }
- }
-